/**
 * 初始化系统参数详情对话框
 */
var AttachmentInfoDlg = {
    attachmentInfoData : new FormData(),
    fileChanged:false
};

/**
 * 清除数据
 */
AttachmentInfoDlg.clearData = function() {
    this.attachmentInfoData =  new FormData();
}

AttachmentInfoDlg.openFileChooser = function(){
	$("#fileChooser").click();
}
AttachmentInfoDlg.changeFileInfo = function(){
	var fileChooser = $("#fileChooser")[0];
	this.fileChanged=true;
	$("#fileName").val("");
	$("#fileSize").val("");
	$("#fileType").val("");
	if(fileChooser.files.length>0){
		var fileItem =fileChooser.files[0];
		$("#fileName").val(fileItem.name);
		$("#fileSize").val(fileItem.size>1024*1024?(Math.round(fileItem.size/1024/1024)+"MB"):(fileItem.size>1024?(Math.round(fileItem.size/1024)+"KB"):(fileItem.size+"B")));
		$("#fileType").val(fileItem.type.substring(fileItem.type.lastIndexOf("/")+1));
		
	}
}


/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
AttachmentInfoDlg.set = function(key, val) {
    this.attachmentInfoData.set(key, (typeof val == "undefined") ? $("#" + key).val() : val);
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
AttachmentInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
AttachmentInfoDlg.close = function() {
    parent.layer.close(window.parent.Attachment.layerIndex);
}

/**
 * 收集数据
 */
AttachmentInfoDlg.collectData = function() {
    this
    .set('id')
    .set('saveTo')
    .set('description')
    .set('file',$('#fileChooser')[0].files[0]);
}

/**
 * 提交添加
 */
AttachmentInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/api/attachment/upload", function(data){
		if(data.success === true){
	        Feng.success("添加成功!");
	        window.parent.Attachment.table.refresh();
	        AttachmentInfoDlg.close();
		}else{
			
        	Feng.error("添加失败!" + data.msg + "!");
		}
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.setProcessData(false)
    .setContentType(false)
    .setData(this.attachmentInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
AttachmentInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/api/attachment/update", function(data){
        if(data.success === true){
	        Feng.success("修改成功!");
	        window.parent.Attachment.table.refresh();
	        AttachmentInfoDlg.close();
		}else{
			
        	Feng.error("修改失败!" + data.msg + "!");
		}
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.setProcessData(false)
    .setContentType(false)
    .setData(this.attachmentInfoData);
    ajax.start();
}

$(function() {
	var fileSize = $("#fileSize").val();
	if(!!fileSize){
		$("#fileSize").val(fileSize>1024*1024?(Math.round(fileSize/1024/1024)+"MB"):(fileSize>1024?(Math.round(fileSize/1024)+"KB"):(fileSize+"B")));
	}
});
